package com.igeek.health.controller;


import com.igeek.health.constant.MessageConstant;
import com.igeek.health.dto.PageDTO;
import com.igeek.health.entity.Checkitem;
import com.igeek.health.result.PageResult;
import com.igeek.health.result.Result;
import com.igeek.health.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 检查项
 * @Author chenmin
 * @Version 1.0
 * @Date 2023/2/14 15:30
 *
 * @RestController 复合注解
 * 1.@Controller
 * 2.@ResponseBody
 * 标注在类上，代表当前Controller中所有方法，返回值都是json数据
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    //查询所有检查项
    @GetMapping("findAll")
    public Result findAll(){
        List<Checkitem> list = checkItemService.findAll();
        if(list!=null && list.size()>0){
            return new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS , list);
        }
        return new Result(false , MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    //根据条件，实现检查项分页查询
   @GetMapping("findPage")
   public PageResult findPage(PageDTO pageDTO){
       PageResult pageResult = checkItemService.findPage(pageDTO);
       return pageResult;
   }

    //新增检查项
    @PostMapping("add")
    public Result add(@RequestBody Checkitem checkitem){
        try {
            checkItemService.add(checkitem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false , MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true , MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //通过检查项ID查询信息
    @GetMapping("{id}")
    public Result viewOne(@PathVariable("id") Integer id){
        Checkitem checkitem = checkItemService.viewOne(id);
        if(checkitem!=null){
            return new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS , checkitem);
        }
        return new Result(false , MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    //修改检查项信息
    @PutMapping("update")
    public Result update(@RequestBody Checkitem checkitem){
        try {
            checkItemService.update(checkitem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false , MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true , MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    //根据检查项ID删除
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        try {
            checkItemService.delete(id);
        }catch (RuntimeException e){
            return new Result(false , e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false , MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true , MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
}
